package com.augurit.common.system.convert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.augurit.common.base.extmodel.base.BaseTreeNode;
import com.augurit.common.base.extmodel.base.LinkCheckTreeNode;
import com.augurit.common.base.extmodel.inner.AppTreeNode;
import com.augurit.common.entity.AcApplication;
import com.augurit.common.entity.AcServer;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.OrgUtils;
import com.augurit.common.system.web.org.form.AcApplicationForm;
import com.augurit.common.system.web.org.form.AcFunctionForm;
import com.augurit.common.system.web.org.form.AcModuleForm;
import com.augurit.common.system.web.org.form.AcServerForm;

public class AcApplicationConverter {

	public static void convertToEntity(AcApplicationForm form, AcApplication entity){
		if(form != null && entity != null){
			
			if(form.getAppId() != null)
				entity.setAppId(form.getAppId());
			
			if(form.getAppCode() != null && form.getAppCode().trim().length() > 0)
				entity.setAppCode(form.getAppCode());
			
			if(form.getAppName() != null && form.getAppName().trim().length() > 0)
				entity.setAppName(form.getAppName());
			
			if(form.getAppType() != null && form.getAppType().trim().length() > 0)
				entity.setAppType(form.getAppType());
			
			if(form.getAppUrl() != null && form.getAppUrl().trim().length() > 0)
				entity.setAppUrl(form.getAppUrl());
			
			entity.setServId(form.getServId());
			
			if(form.getIsActive() != null && form.getIsActive().trim().length() > 0)
				entity.setIsActive(form.getIsActive());
			
			if(form.getAppDesc() != null && form.getAppDesc().trim().length() > 0)
				entity.setAppDesc(form.getAppDesc());
			
			if(form.getRemark() != null && form.getRemark().trim().length() > 0)
				entity.setRemark(form.getRemark());
		}
	}
	
	public static AcApplicationForm convertToForm(AcApplication entity){
		if(entity != null){
			AcApplicationForm form = new AcApplicationForm();
			
			form.setAppId(entity.getAppId());
			form.setAppCode(entity.getAppCode());
			form.setAppName(entity.getAppName());
			form.setAppType(entity.getAppType());
			form.setAppUrl(entity.getAppUrl());
			form.setServId(entity.getServId());
			form.setIsActive(entity.getIsActive());
			form.setAppDesc(entity.getAppDesc());
			form.setRemark(entity.getRemark());
			
			return form;
		}else
			return null;
	}
	
	/**
	 * 将应用实体对象列表转换为应用form对象列表
	 * @param result
	 * @return
	 */
	public static List<AcApplicationForm> convertToFormList(List<AcApplication> list){
		return convertToFormList(list, null, null);
	}
	
	/**
	 * 
	 * @param list
	 * @return
	 */
	public static List<AcApplicationForm> convertToFormList(List<AcApplication> list, List<AcServerForm> acServerList, List<SysCodeForm> appTypeList){
		if(list != null && list.size() > 0){
			List<AcApplicationForm> result = new ArrayList();
			
			for(AcApplication entity : list){
				AcApplicationForm form = convertToForm(entity);
				
				form.setAppTypeLabel(OrgUtils.getItemName(form.getAppType(), appTypeList));
				OrgUtils.setAcServerInfoToApp(form, acServerList);
				
				result.add(form);
			}
			
			return result;
		}else
			return null;
	}

	/**
	 * 将应用Form对象列表转换为ExtTreeNode对象列表
	 * @param result
	 * @return
	 */
	public static List<AppTreeNode> convertAppToTreeNode(List<AcApplicationForm> list){
		List<AppTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(AcApplicationForm org : list)
				result.add(convertAppToTreeNode(org));
		}
		return result;
	}
	
	
	/**
	 * 递归构造树
	 * @param node AppTreeNode树结点
	 * @param moduleList 模块form列表
	 */
	private static void constructTree(LinkCheckTreeNode parentNode, List<AcModuleForm> moduleList, Long[] funcIds){
		boolean flag = false;
		
		if(funcIds != null && funcIds.length > 0){
			flag = true;
		}
			
		
		if(moduleList != null && moduleList.size() > 0){
			//遍历整个模块列表
			for(AcModuleForm currentModule: moduleList){
				
				//如果当前结点的孩子列表为空，需要为它分配空间
				if(parentNode.getChildren() == null){
					parentNode.setChildren(new ArrayList());
				}
				
				//把当前模块转换为AppTreeNode树结点，包含2个步骤：
				//((1)转换它自身
				LinkCheckTreeNode currentNode = AcModuleConverter.convertModuleToCheckTreeNode(currentModule);
				if(currentNode.getChildren() == null){
					currentNode.setChildren(new ArrayList());
				}
				
				int numOfFuncs = 0;
				// (2)把它的所有功能加到它的孩子列表
				List<AcFunctionForm> functions = currentModule.getFunctions();
				if(functions != null && functions.size() > 0){
					for(AcFunctionForm currentFunc:functions){
						LinkCheckTreeNode fucNode = AcFunctionConverter.convertFuncToCheckTreeNode(currentFunc);
						
						if(flag){
							if(Arrays.binarySearch(funcIds, currentFunc.getFuncId()) >= 0){
								fucNode.setChecked(true);
								numOfFuncs++;
							}
						}
						currentNode.getChildren().add(fucNode);
					}
				}
				if(functions != null){
					if(numOfFuncs != 0 && numOfFuncs == functions.size()){
						currentNode.setChecked(true);
					}
				}
				
			    //把当前模块转换后的AppTreeNode树结点加到它的父节点的孩子中
				parentNode.getChildren().add( currentNode );
				
				//递归调用,currentNode重新成为父节点
				constructTree(currentNode,currentModule.getChildren(),funcIds);
			}
		}
	}
	
	/**
	 * 递归构造树
	 * @param node AppTreeNode树结点
	 * @param moduleList 模块form列表
	 * @param appId 应用id
	 * @param appUrl 应用url
	 */
	private static void constructSimpleSelTree(BaseTreeNode parentNode, List<AcModuleForm> moduleList, Long appId, String appUrl){
		if(moduleList != null && moduleList.size() > 0){
			//遍历整个模块列表
			for(AcModuleForm currentModule : moduleList){
				//如果当前结点的孩子列表为空，需要为它分配空间
				if(parentNode.getChildren() == null){
					parentNode.setChildren(new ArrayList());
				}
				
				//把当前模块转换为AppTreeNode树结点，包含2个步骤：
				//((1)转换它自身
				BaseTreeNode currentNode = AcModuleConverter.convertModuleToExtTreeNode(currentModule);
				if(currentNode.getChildren() == null){
					currentNode.setChildren(new ArrayList());
				}
				
				// (2)把它的所有功能加到它的孩子列表
				List<AcFunctionForm> functions = currentModule.getFunctions();
				if(functions != null && functions.size() > 0){
					for(AcFunctionForm currentFunc:functions){
						 
						LinkCheckTreeNode fucNode = AcFunctionConverter.convertFuncToCheckTreeUrlNode(currentFunc, appId, appUrl);
						currentNode.getChildren().add(fucNode);
					}
				}
				
			    //把当前模块转换后的AppTreeNode树结点加到它的父节点的孩子中
				parentNode.getChildren().add( currentNode );
				
				//递归调用,currentNode重新成为父节点
				constructSimpleSelTree(currentNode, currentModule.getChildren(), appId, appUrl);
			}
		}
	}
	
	/**
	 * 一次性的把应用转换为一棵树
	 * @param form 应用form对象
	 * @param funcIds 功能id数组 
	 * @return AppTreeNode树结点
	 */
	public static LinkCheckTreeNode OneTimeConvertAppToTree(AcApplicationForm form, Long[] funcIds){
		LinkCheckTreeNode node = null;
		
		if(form != null){
			node = convertAppToCheckTreeNode(form);
			
			if(form.getChildren() != null){
				constructTree(node, form.getChildren(), funcIds);
			}
			
			int numOfModules = 0;
			List<LinkCheckTreeNode> moduleNodes = node.getChildren();
			if(moduleNodes != null && moduleNodes.size() > 0){
				for(LinkCheckTreeNode moduleNode:moduleNodes){
					if(moduleNode.isChecked()){
						numOfModules++;
					}
				}
				if(numOfModules != 0 && numOfModules == moduleNodes.size())
					node.setChecked(true);
			}
		}
		
		return node;
	}
	
	/**
	 * 一次性的把应用转换为一棵树
	 * @param form 应用form对象
	 * @param funcIds 功能id数组 
	 * @return AppTreeNode树结点
	 */
	public static BaseTreeNode OneTimeConvertAppToSimpleSelTree(AcApplicationForm form){
		BaseTreeNode node = null;
		Long appId = form.getAppId();
		String appUrl = form.getAppUrl();
		if(form != null){
			node = convertAppToExtTreeNode(form);
			if(form.getChildren() != null){
				constructSimpleSelTree(node, form.getChildren(), appId, appUrl);
			}
		}
		return node;
	}
	
	/**
	 * 一次性的把所有的应用转换为一棵树（包括应用、应用下的模块、模块下功能）
	 * @param list 应用列表
	 * @param funcIds 功能id数组
	 * @return 应用树节点列表
	 */
	public static List<LinkCheckTreeNode> OneTimeConvertAllAppsToTree(List<AcApplicationForm> list, Long[] funcIds){
		if(list != null && list.size() > 0){
			
			if(funcIds != null && funcIds.length > 0){
				//功能ID排序,为二分查找准备
				Arrays.sort(funcIds);
			}
			
			List<LinkCheckTreeNode> result = new ArrayList();
			
			for(AcApplicationForm form : list){
				LinkCheckTreeNode node = OneTimeConvertAppToTree(form, funcIds);
				result.add(node);
			}
			
			return result;
		}
		return null;
	}
	
	/**
	 * 一次性的把所有的应用转换为一棵树（包括应用、应用下的模块、模块下功能）
	 * @param list 应用列表
	 * @return 应用树节点列表
	 */
	public static List<BaseTreeNode> OneTimeConvertAllAppsToSimpleSelTree(List<AcApplicationForm> list){
		List<BaseTreeNode> result = null;
		if(list != null && list.size() > 0){
			result = new ArrayList<BaseTreeNode>();
			for(AcApplicationForm form:list){
				BaseTreeNode node = OneTimeConvertAppToSimpleSelTree(form);
				result.add(node);
			}
		}
		return result;
	}
	
	/**
	 * 将应用Form对象列表转换为ExtTreeNode对象列表
	 * @param result
	 * @return
	 */
	public static List<AppTreeNode> convertModuleToTreeNode(List<AcModuleForm> list){
		List<AppTreeNode> result = new ArrayList();
		if(list != null && list.size() > 0){
			for(AcModuleForm module : list)
				result.add(AcModuleConverter.convertModuleToTreeNode(module));
		}
		return result;
	}


	/**
	 * 将应用Form对象转换为ExtCheckTreeNode对象
	 * @param form
	 * @return
	 */
	public static LinkCheckTreeNode convertAppToCheckTreeNode(AcApplicationForm form){
		if(form != null){
			LinkCheckTreeNode node = new LinkCheckTreeNode();
			node.setId(AppTreeNode.getKey(AcApplication.class.getSimpleName(), form.getAppId()));
			node.setText(form.getAppName());
			node.setIconCls(AppTreeNode.CLS_APP);
			node.setLeaf(form.getChildren() != null ? false : true);
			return node;
		}else
			return null;
	}
	
	/**
	 * 将应用Form对象转换为ExtTreeNode对象
	 * @param form
	 * @return
	 */
	public static BaseTreeNode convertAppToExtTreeNode(AcApplicationForm form){
		if(form != null){
			BaseTreeNode node = new BaseTreeNode();
			node.setId(AppTreeNode.getKey(AcApplication.class.getSimpleName(), form.getAppId()));
			node.setText(form.getAppName());
			node.setIconCls(AppTreeNode.CLS_APP);
			node.setLeaf(form.getChildren() != null ? false : true);
			return node;
		}else
			return null;
	}
	
	/**
	 * 将应用Form对象转换为AppTreeNode对象
	 * @param form
	 * @return
	 */
	public static AppTreeNode convertAppToTreeNode(AcApplicationForm form){
		if(form != null){
			AppTreeNode node = new AppTreeNode();
			node.setId(AppTreeNode.getKey(AcApplication.class.getSimpleName(), form.getAppId()));
			node.setText(form.getAppName());
			node.setQtip(form.getAppCode());
			node.setIconCls(AppTreeNode.CLS_APP);
			node.setLeaf(false);
			return node;
		}else
			return null;
	}
}
